#ifndef _MCP61_H_
#define _MCP61_H_

#include "cap_afe.h"

#define GPIOI2C_SCL_GPIO_PORT          GPIOA
#define GPIOI2C_SCL_PIN                GPIO_Pin_8
#define I2C1_SCL_BUSCLK                RCC_AHBENR_GPIOA
#define GPIOI2C_SCL_GPIO_CLK_ENABLE()  RCC_AHBPeriphClockCmd(GPIOI2C_SCL_BUSCLK, ENABLE);

#define GPIOI2C_SDA_GPIO_PORT          GPIOA
#define GPIOI2C_SDA_PIN                GPIO_Pin_9
#define I2C1_SDA_BUSCLK                RCC_AHBENR_GPIOA
#define GPIOI2C_SDA_GPIO_CLK_ENABLE()  RCC_AHBPeriphClockCmd(GPIOI2C_SDA_BUSCLK, ENABLE);

#define SCL_H                       GPIOI2C_SCL_GPIO_PORT->BSRR = GPIOI2C_SCL_PIN
#define SCL_L                       GPIOI2C_SCL_GPIO_PORT->BRR  = GPIOI2C_SCL_PIN

#define	SDA_H	                      GPIOI2C_SDA_GPIO_PORT->BSRR	= GPIOI2C_SDA_PIN
#define SDA_L	                      GPIOI2C_SDA_GPIO_PORT->BRR	= GPIOI2C_SDA_PIN

#define MY_DELAY_US(X)   Delay_us(X)

/* Definition of Macros for bit operations */
#define i2c_set_scl()      { GPIOI2C_SCL_GPIO_PORT->BSRR = GPIOI2C_SCL_PIN; }
#define i2c_clear_scl()    { GPIOI2C_SCL_GPIO_PORT->BRR = GPIOI2C_SCL_PIN; }

#define i2c_set_sda()      { GPIOI2C_SDA_GPIO_PORT->BSRR = GPIOI2C_SDA_PIN; }
#define i2c_clear_sda()    { GPIOI2C_SDA_GPIO_PORT->BRR = GPIOI2C_SDA_PIN; }
#define i2c_get_sda()      ( GPIOI2C_SDA_GPIO_PORT->IDR & GPIOI2C_SDA_PIN )
#define i2c_get_scl()      ( GPIOI2C_SCL_GPIO_PORT->IDR & GPIOI2C_SCL_PIN )
#define sda_wr_control(b)  { if(b & mask) i2c_set_sda() else i2c_clear_sda(); }
#define sda_rd_control(b)  { if(i2c_get_sda() != 0) b |= mask; }

#define i2c_set_vdd()      { GPIOI2C_VDD_GPIO_PORT->BSRR = GPIOI2C_VDD_PIN; }
#define i2c_clear_vdd()    { GPIOI2C_VDD_GPIO_PORT->BRR = GPIOI2C_VDD_PIN; }

/** @defgroup  
  * I2C transaction result
  */ 
#define GPIOI2C_XFER_LASTNACK         ((uint8_t)0x00)   	 	/* !< No error              */
#define GPIOI2C_XFER_ADDRNACK   			((uint8_t)0x01)    	  /* !< No Device             */
#define GPIOI2C_XFER_ABORTNACK  			((uint8_t)0x02)    	  /* !< NACK before last byte */
#define GPIOI2C_XFER_LASTACK    			((uint8_t)0x04)     	/* !< ACK last byte         */
#define GPIOI2C_XFER_BUSERR     			((uint8_t)0x10)     	/* !< Bus error             */
#define GPIOI2C_XFER_BUSBUSY     			((uint8_t)0x20)     	/* !< Bus busy              */

/* Definition of GPIOI2C constants */
#define GPIOI2C_ACK  				((uint8_t)1)
#define GPIOI2C_NACK 				((uint8_t)0)
#define I2CWRITE_MASK  			((uint8_t)0xFE)
#define I2CREAD_MASK   			((uint8_t)0x01)
/* Private variables ---------------------------------------------------------*/

#define SCL_STRETCH_TIMEOUT						1000000


typedef enum
{
  GPIOI2C_BUS_RESET         	= 0x10,   /*!< Peripheral is not yet Initialized         */
  GPIOI2C_BUS_IDLE              = 0x00,   /*!< Peripheral Initialized and ready for use  */
  GPIOI2C_BUS_BUSY              = 0x01,   /*!< An transaction process is ongoing         */   
  GPIOI2C_BUS_ERROR             = 0x20    /*!< Error                                     */ 
}GPIOI2C_BusStateDef;

#define MCO_PIN_OUT(X)	{ \
							GPIOA->CRL &=~((uint32_t)0xf<<28);  \
							GPIOA->CRL |=(uint32_t)0x00<<28; \
							if(!X) \
								GPIOA->BRR |= GPIO_Pin_7; \
							else \
								GPIOA->BSRR |= GPIO_Pin_7; \
						}

#define SD_PIN GPIO_Pin_6
			
GPIOI2C_BusStateDef GPIOI2C_Bus_Init(void);
			
void GPIOI2C_Bus_DeInit(void);

/***********************************************************************************************************************
  * @brief  Initialize CAP AFE
  * @note   none
  * @param  Cap_Structure
  * @retval none
  *********************************************************************************************************************/
void Cap_Afe_Init(CAP_AFE* Cap_Structure);

/***********************************************************************************************************************
  * @brief  Initialize VT
  * @note   none
  * @param  Cap_Structure
  * @retval none
  *********************************************************************************************************************/
void VT_Init(CAP_AFE* Cap_Structure);
						
void Conf_FreDiv(CAP_AFE* Cap_Structure);

#endif

